﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KZ400B.Controls.TPlan
{
    
    /// <summary>
    /// TPlanAdd.xaml 的交互逻辑
    /// </summary>
    public partial class TPlanEdit : UserControl
    {
        public event UserControlClose OnClose;
        public TPlanEdit()
        {
            InitializeComponent();
        }

        public static DependencyProperty TPlanIDProperty = DependencyProperty.Register(
      "TPlanID", typeof(int)
      , typeof(TPlanEdit)
      , new PropertyMetadata(0, new PropertyChangedCallback(OnTPlanIDChange))
      );

        //private int m_TPlanID;
        public int TPlanID
        {
            get { return (int)GetValue(TPlanIDProperty); }
            set
            {
                SetValue(TPlanIDProperty, value);
                //m_TPlanID = value;
            }
        }

        private static void OnTPlanIDChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TPlanEdit control = (TPlanEdit)d;
            control.bind();
        }

        void bind()
        { 
            DB.Model.TPlan tp = m_tManager.getModel(TPlanID);
            txtTPName.Text = tp.Name;
            txtTotalLoopTimes.Text = tp.LoopTimes.ToString();
            xControl.TPlanID = TPlanID;
            yControl.TPlanID = TPlanID;
            zControl.TPlanID = TPlanID;
            tControl.TPlanID = TPlanID;
            uControl.TPlanID = TPlanID;
            wControl.TPlanID = TPlanID;
            
            List<DB.Model.TPlanAxle> aList = tp.AxleList;
            if (aList != null && aList.Count > 0)
            {
                foreach (DB.Model.TPlanAxle ax in aList)
                {
                    if (ax.AxleName == "X")
                    {
                        setAxleModel(ref xControl, ax);
                    }
                    else if (ax.AxleName == "Y")
                    {
                        setAxleModel(ref yControl, ax);
                    }
                    else if (ax.AxleName == "Z")
                    {
                        setAxleModel(ref zControl, ax);
                    }
                    else if (ax.AxleName == "T")
                    {
                        setAxleModel(ref tControl, ax);
                    }
                    else if (ax.AxleName == "U")
                    {
                        setAxleModel(ref uControl, ax);
                    }
                    else if (ax.AxleName == "W")
                    {
                        setAxleModel(ref wControl, ax);
                    }
                }
            }
            

        }

        void setAxleModel(ref TPlanAxle control, DB.Model.TPlanAxle ax)
        {
            //if (ax != null && control != null)
            //{
            //    control.TPlanID = this.TPlanID;
            //    control.IsEnabled = true;
            //    //control.AxleName = ax.AxleName;
            //    control.StartPostionText = ax.StartPosition.ToString();
            //    control.RunningUnitValue = ax.RunUnit;
            //    //control.IsChecked = true;
            //}
        }

        TPlanManager m_tManager = new TPlanManager();
        DB.Model.TPlanAxle getAxleModel(TPlanAxle control)
        {
            DB.Model.TPlanAxle ax = null;
            if (control != null && control.IsEnabled)
            {
                ax = new DB.Model.TPlanAxle();
                ax.StartPosition = control.StartPostion;
                //ax.RunUnit = (int)control.RunningUnit;
                ax.AxleName = control.AxleName;
                ax.AxleID = control.AxleID;
            }
            return ax;
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtTPName.Text))
            {
                MessageBox.Show("请输入方案名称");
                return;
            }

            if (string.IsNullOrEmpty(txtTotalLoopTimes.Text))
            {
                MessageBox.Show("请输入总循环次数");
                return;
            }

            DB.Model.TPlan plan = m_tManager.getModel(TPlanID);
            if (plan == null)
            {
                MessageBox.Show("没有找到方案");
                return;
            }
            plan.Name = txtTPName.Text;
            int iValue = 0;
            if (int.TryParse(txtTotalLoopTimes.Text, out iValue))
            {
                plan.LoopTimes = iValue;
            }
            List<DB.Model.TPlanAxle> aList = new List<DB.Model.TPlanAxle>();
            aList.Add(getAxleModel(xControl));
            aList.Add(getAxleModel(yControl));
            aList.Add(getAxleModel(zControl));
            aList.Add(getAxleModel(tControl));
            aList.Add(getAxleModel(uControl));
            aList.Add(getAxleModel(wControl));
            try
            {
                bool rst = m_tManager.update(plan, aList);
                MessageBox.Show(string.Format("修改{0}", rst ? "成功" : "失败"));
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("修改失败:{0}", ex.Message));
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            if (OnClose != null)
            {
                OnClose();
            }
        }
    }
}
